home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1999 March / EnigmA AMIGA RUN 35 (1999)(G.R. Edizioni)(IT)[!][issue 1999-03].iso / earkit / mail / thor / thor25_api.lha / WatchTHOR / main.c next >
C/C++ Source or Header  |  1997-04-06  |  7KB  |  317 lines

  1. #include "watch.h"
  2. #include "watchthor_rev.h"
  3.  
  4. static UBYTE *version_string = VERSTAG;
  5.  
  6. /*  Prototypes for external functions  */
  7.  
  8. struct Node * FindiName(struct TaskData *td, struct List *list, STRPTR name);
  9. BOOL SetupTime(struct TaskData *td, UBYTE **error);
  10. void CloseTime(struct TaskData *td);
  11. void TimeRequest(struct TaskData *td, ULONG mins);
  12. LONG myEasyRequest(struct IntuitionBase *IntuitionBase, struct Window *w, struct EasyStruct *es, ULONG *ip, ULONG arg1, ...);
  13. struct Window * GetFrontScreenWindow(struct IntuitionBase *IntuitionBase);
  14.  
  15. UBYTE *template = "SYSTEMNAME=SYSTEM,CHECKDELAY=PERIODIC/N,CONFERENCE=CONF/K,RUNCOMMAND=CMD/K,USAGE/S";
  16.  
  17. enum {
  18.     TEM_BBSNAME,
  19.     TEM_CHECKDELAY,
  20.     TEM_CONFNAME,
  21.     TEM_COMMAND,
  22.     TEM_USAGE,
  23.     TEM_NUMBEROF
  24. };
  25.  
  26. LONG __saveds NoName(void)
  27. {
  28.     TEXT envbuf[256];
  29.     struct TaskData TaskData, *td;
  30.     struct RDArgs *rdargs = NULL;
  31.     LONG array[TEM_NUMBEROF], retval = RETURN_FAIL, n;
  32.     struct List *bbslist = NULL, *conflist = NULL;
  33.     BOOL terminated = FALSE;
  34.     ULONG signal, signalmask, checkdelay, oldlastmail;
  35.     struct BBSListItem    *mybbs;
  36.     struct ConfListItem    *myconf;
  37.     UBYTE                *errmsg = NULL;
  38.     struct IntuitionBase *IntuitionBase = NULL;
  39.  
  40.     /*
  41.         This program is meant to be pure so we have all data that would
  42.         normally be global in a separate taskdata structure, which we
  43.         initialize here
  44.     */
  45.  
  46.     setmem(td = &TaskData, sizeof(struct TaskData), 0);
  47.     
  48.     SysBase = *(struct ExecBase **)4L;
  49.  
  50.     if(!(DOSBase = (struct DosLibrary *) OpenLibrary(DOSNAME, 0L)))
  51.     {
  52.         errmsg = "missing library";        /* Should never happen */
  53.         retval = 10000L;
  54.         goto quit;
  55.     }
  56.     if(DOSBase->dl_lib.lib_Version < 37)
  57.     {
  58.         errmsg = "Needs Kick V37+";
  59.         goto quit;
  60.     }
  61.  
  62.     if(!(UtilityBase = OpenLibrary("utility.library", 37L)))
  63.     {
  64.         errmsg = "Needs utility.library V37+";
  65.         goto quit;
  66.     }
  67.  
  68.     if(!(IntuitionBase = (struct IntuitionBase *)OpenLibrary("intuition.library", 37L)))
  69.     {
  70.         errmsg = "Needs intuition.library V37+";
  71.         goto quit;
  72.     }
  73.  
  74.     /* Open bbsread.library relative to the directory THOR resides in */
  75.  
  76.     if(GetVar(ENV_THORPATH, envbuf, 255, GVF_GLOBAL_ONLY) != -1)
  77.     {
  78.         if(AddPart(envbuf, "libs/" BBSREADNAME, 200))
  79.         {
  80.             BBSReadBase = OpenLibrary(envbuf, 3L);
  81.         }
  82.     }
  83.     if(!BBSReadBase) 
  84.     {
  85.         if(!(BBSReadBase = OpenLibrary(BBSREADNAME, 3L)))
  86.         {
  87.             errmsg = "Needs bbsread.library V3+";
  88.             goto quit;
  89.         }
  90.     }
  91.  
  92.     for(n = 0; n < TEM_NUMBEROF; array[n++] = 0L);
  93.  
  94.     if(!(rdargs = ReadArgs(template, array, NULL)))
  95.     {
  96.         errmsg = "Error in arguments.";
  97.         goto quit;
  98.     }
  99.  
  100.     if(array[TEM_USAGE] || !array[TEM_BBSNAME]) 
  101.     {
  102.         errmsg = "Usage: WatchTHOR [SYSTEMNAME] [CONFNAME] [CHECKDELAY] [RUNCOMMAND] [USAGE]\n       Copyright (C) 1995 Ultima Thule Software, All Rights Reserved.\n       Author: Petter Nilsen.";
  103.         retval = RETURN_OK;
  104.         goto quit;
  105.     }
  106.  
  107.     /* Set up how often it should check for new mail */
  108.  
  109.     checkdelay = array[TEM_CHECKDELAY] ? *((ULONG *)array[TEM_CHECKDELAY]) : 5;
  110.  
  111.     if(checkdelay == 0)
  112.         checkdelay = 1;
  113.  
  114.     /* Get a list of systems configured in THOR */
  115.  
  116.     if(!(bbslist = GetBBSList())) { errmsg = "Error getting bbslist."; goto quit; }
  117.  
  118.     /* Find our system to watch */
  119.  
  120.     if(!(mybbs = (struct BBSListItem *)FindiName(td, bbslist, (STRPTR) array[TEM_BBSNAME])))
  121.     {
  122.         errmsg = "Unknown system";
  123.         goto quit;
  124.     }
  125.  
  126.     /* Get a list of conferences on this system */
  127.  
  128.     if(!(conflist = GetConfList(mybbs))) { errmsg = "Error getting conflist."; goto quit; }
  129.  
  130.     /* Find our conference to watch */
  131.  
  132.     if(!(myconf = (struct ConfListItem *)FindiName(td, conflist, array[TEM_CONFNAME] ? (STRPTR)array[TEM_CONFNAME] : (STRPTR)"EMail")))
  133.     {
  134.         errmsg = "Unknown conference";
  135.         goto quit;
  136.     }
  137.  
  138.     /* Setup timer */
  139.  
  140.     if(!(SetupTime(td, &errmsg)))
  141.         goto quit;
  142.  
  143.  
  144.     /* 
  145.         We only need to monitor the last message in that conference, 
  146.         when it changes, new messages have arrived
  147.     */
  148.  
  149.     oldlastmail = myconf->cl_Internal->ci_LastMsg;
  150.  
  151.     /* Start the first timer request */
  152.  
  153.     TimeRequest(td, checkdelay);
  154.  
  155.     /* Set up the signal mask for Wait() */
  156.  
  157.     signalmask = (1 << timerport->mp_SigBit | SIGBREAKF_CTRL_C);
  158.  
  159.     while(!terminated)
  160.     {
  161.         signal = Wait(signalmask);
  162.  
  163.         if(signal & SIGBREAKF_CTRL_C)
  164.         {
  165.             terminated = TRUE;
  166.         }
  167.         if(signal & (1 << timerport->mp_SigBit))
  168.         {
  169.             /* 
  170.                 Update our conference pointer to notice any changes
  171.                 in the structure. It will not work otherwise!
  172.             */
  173.  
  174.             UpdateDataStructTags(UD_ConfItem, myconf, TAG_DONE);
  175.  
  176.             if(myconf->cl_Internal->ci_LastMsg != oldlastmail)
  177.             {
  178.                 struct EasyStruct es;
  179.                 STRPTR text = "%ld new messages have arrived in \n"
  180.                             "conference '%s' on system '%s'";
  181.  
  182.                 if(array[TEM_COMMAND])
  183.                 {
  184.                     System((STRPTR)array[TEM_COMMAND], TAG_DONE);        
  185.                 }
  186.                 else
  187.                 {
  188.                     es.es_StructSize = sizeof(struct EasyStruct);
  189.                     es.es_Flags = 0L;
  190.                     es.es_Title = "WatchTHOR Notification";
  191.                     es.es_TextFormat =  text; 
  192.                     es.es_GadgetFormat = "Ok";
  193.  
  194.                     myEasyRequest(IntuitionBase, GetFrontScreenWindow(IntuitionBase), &es, NULL,
  195.                                     myconf->cl_Internal->ci_LastMsg - oldlastmail,
  196.                                     myconf->cl_Data->cd_Name,
  197.                                     mybbs->bl_Data->bd_Name);
  198.  
  199.                 }
  200.                 oldlastmail = myconf->cl_Internal->ci_LastMsg;
  201.             
  202.             }
  203.  
  204.             /* Done with this request, start a new one */
  205.  
  206.             TimeRequest(td, checkdelay);
  207.         }
  208.     }
  209.  
  210.  
  211. done:   /*  All stuff is done  */
  212.  
  213.     retval = RETURN_OK;
  214.  
  215.     Flush(Output());
  216.  
  217. quit:
  218.     CloseTime(td);
  219.  
  220.     if(errmsg)
  221.     {
  222.         Printf("%s\n", errmsg);
  223.     };
  224.  
  225.     Flush(Output());
  226.  
  227.     if(conflist) FreeBRObject(conflist);
  228.     if(bbslist) FreeBRObject(bbslist);
  229.     if(rdargs) FreeArgs(rdargs);
  230.     CloseLibrary(BBSReadBase);
  231.     CloseLibrary((struct Library *)IntuitionBase);
  232.     CloseLibrary((struct Library *)UtilityBase);
  233.     CloseLibrary((struct Library *)DOSBase);
  234.     return(retval);
  235. }
  236.  
  237. /************************************************************************/
  238.  
  239. struct Node * FindiName(struct TaskData *td, struct List *list, STRPTR name)
  240. {
  241.     struct Node *node = (struct Node *) &list->lh_Head;
  242.  
  243.     while((node = node->ln_Succ)->ln_Succ)
  244.     {
  245.         if(!Stricmp(name, node->ln_Name)) return(node);
  246.     }
  247.     return(NULL);
  248. }
  249.  
  250. LONG myEasyRequest(
  251.     struct IntuitionBase *IntuitionBase,
  252.     struct Window *w,
  253.     struct EasyStruct *es,
  254.     ULONG *ip,
  255.     ULONG arg1, ...)
  256. {
  257.     return(EasyRequestArgs(w, es, ip, &arg1));
  258. }
  259.  
  260. /**************************************************/
  261. /******************* Timer stuff ******************/
  262. /**************************************************/
  263.  
  264. BOOL SetupTime(struct TaskData *td, UBYTE **error)
  265. {
  266.     if(!(timerport = CreateMsgPort()))
  267.     {
  268.         *error = "Couldn't create message port.";
  269.         return(FALSE);
  270.     }
  271.     timerio = (struct timerequest *)CreateIORequest(timerport, sizeof(struct timerequest));
  272.  
  273.     if(OpenDevice(TIMERNAME, UNIT_VBLANK, (struct IORequest *)timerio, 0))
  274.     {
  275.         *error = "Couldn't open timer device.";
  276.         return(FALSE);
  277.     }
  278.     return(TRUE);
  279. }
  280.  
  281. void CloseTime(struct TaskData *td)
  282. {
  283.     if(timerport)
  284.     {
  285.         if(timerio)
  286.         {
  287.             AbortIO((struct IORequest *)timerio);
  288.             WaitIO((struct IORequest *)timerio);
  289.             CloseDevice((struct IORequest *)timerio);
  290.             DeleteIORequest((struct IORequest *)timerio);
  291.             timerio = NULL;
  292.         }
  293.         DeleteMsgPort(timerport);
  294.         timerport = NULL;
  295.     }
  296. }
  297.  
  298. void TimeRequest(struct TaskData *td, ULONG mins)
  299. {
  300.     timerio->tr_node.io_Command    = TR_ADDREQUEST;
  301.     timerio->tr_time.tv_secs    = (60 * mins);
  302.     timerio->tr_time.tv_micro    = 0;
  303.     SendIO((struct IORequest *)timerio);
  304. }
  305.  
  306. struct Window * GetFrontScreenWindow(struct IntuitionBase *IntuitionBase)
  307. {
  308.     struct Screen *actscr;
  309.     ULONG iblock;
  310.  
  311.     iblock = LockIBase(0L);
  312.     actscr = IntuitionBase->ActiveScreen;
  313.     UnlockIBase(iblock);
  314.  
  315.     return(actscr->FirstWindow);
  316. }
  317.